<template>
  <div class="home">
    <!-- 左侧 -->
    <el-container>
      <el-aside width="auto">
        <!-- logo -->
        <div class="logo"></div>
          <el-menu background-color="#F9F9F9" default-active="2" class="el-menu-admin" @open="handleOpen" @close="handleClose" :collapse="isCollapse">
            <el-submenu :index="index+'0'" v-for="(firstItem,index) in menusList" :key="index">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{firstItem.authName}}</span>
              </template>
              <el-menu-item-group v-for="(secondItem,index) in firstItem.children" :key="index">
                <el-menu-item :index="index+'1'" @click="jumpTo(secondItem.path)"><i class="el-icon-menu"></i>{{secondItem.authName}}</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
      </el-aside>
      <!-- 右侧 -->
      <el-container>
        <!-- 头部 -->
        <el-header>
          <div class="myicon myicon-menu toggle-btn" @click="isCollapse=!isCollapse"></div>
          <div class="system-title">电商后台管理系统</div>
          <el-button type="warning" round @click="logout">退出</el-button>
        </el-header>
        <!-- 主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { getMenusList } from '@/api'
export default {
  data () {
    return {
      menusList: [],
      isCollapse: false
    }
  },
  created () {
    getMenusList().then(res => {
      // console.log(res)
      this.menusList = res.data
    })
  },
  methods: {
    // 导航菜单展开触发函数
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    // 导航菜单关闭触发函数
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 路由跳转
    jumpTo (pathName) {
      this.$router.push({ path: pathName })
    },
    // 退出登录
    logout () {
      // 删除本地token
      localStorage.removeItem('mytoken')
      // 提示
      this.$message({
        message: '成功退出！！',
        type: 'warning',
        duration: 1000
      })
      setTimeout(() => {
        this.$router.push({ path: '/login' })
      }, 1000)
    }
  }
}
</script>

<style lang="scss" scoped>
html,body{
    height: 100%;
  }
.home {
  height: 100%;
  // 设置.el-menu-admin下所有非.el-menu--collapse的所有元素宽度,这个类只有点击收缩时才会出现
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
    font-weight: 700;
  }
  .welcome, {
    // color: white;
  }
}
.myicon{
    font-size:36px;
  }
</style>
